<? ob_start(); ?>
<?php
 $i = 0;
 $mid="";
 $rid="";
 $url="";
 $urltag = FALSE;
    foreach($_GET as $key => $value) {
        $i++;
		if($key == "ourid") {$mid = $value;}
		if($key == "rid") { $rid = $value; }
		elseif ($urltag == TRUE && (strlen(strstr($url,"?"))==0)) {$url .="?". $key."=".$value;}
		elseif ($urltag == TRUE && (strlen(strstr($url,"?"))>0)) {$url .="&". $key."=".$value;}
		elseif($key == "url") {
		$urltag = TRUE;
		$url .= $value;} 
    } 
 $db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
 $ip=$_SERVER['REMOTE_ADDR'];
 if ($ip == "") {
        $ip0=0;
		$ip1=0;
		$ip2=0;
		$ip3=0;
    } else {
        $ips = split ("\.", "$ip");
        $ip3=$ips[3];
		$ip2=$ips[2];
		$ip1=$ips[1];
		$ip0=$ips[0];
    }
mysql_select_db("JRtu434", $db);
 
 $sql = "select * from tempURLs where rid='" . $rid . "' and mid='" . $mid ."'and url='" . $url ."'";
 $res = mysql_query($sql);
 if(!mysql_num_rows($res)) {
 mysql_query("INSERT INTO tempURLs (mid,rid,url,ip0,ip1,ip2,ip3) VALUES ('".$mid."','".$rid."','".$url."','".$ip0."','".$ip1."','".$ip2."','".$ip3."')");
 $res = mysql_query($sql);
 $item = mysql_fetch_array($res);
 $nurl = $url. $item['id'];
 }
 else
 {
 $res = mysql_query($sql);
 $item = mysql_fetch_array($res);
 $nurl = "http://www.gain-www.com/redirect/okaeri.php?status=quotafull&mid=".$item['id'];
 }
 header("HTTP/1.1 301 Moved Permanently");
 header("Location: " . $nurl);
 mysql_close();
?>
<? ob_flush(); ?> 
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html lang="ja"> 
 
<!-- ==== document header ==== --> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<meta http-equiv="Content-Style-Type" content="text/css"> 
<meta http-equiv="Content-Script-Type" content="text/javascript"> 
<title>アンケート画面に移動します</title> 
</head> 
<?php
 if ($item == false){
  echo "URLが不正です。";
 } else {
  echo "アンケート画面に移動します。<br>画面がかわらない方は、<a href='" . $nurl . "'>こちら</a>をクリックしてください。";
 }
?>
</html>